package com.zhou.utils;

import com.zhou.uploadServer.model.FileUpload;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @ClassName: getFileMsg
 * @Description:
 * @Author: admin
 * @Date: 2021/12/22 21:56
 */
public class GetFileMsg {

    SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");

    public  FileUpload getMsg(MultipartFile file) throws FileNotFoundException {
        FileUpload fileUpload = new FileUpload();
        //获取文件大小,转化为以kb为单位的大小
        Double size = Double.parseDouble(String.valueOf(file.getSize())) / 1024;
        BigDecimal b = new BigDecimal(size);
        size = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        fileUpload.setFileSize(size);


        //文件旧文件名
        String fileName = file.getOriginalFilename();
        fileUpload.setOldName(fileName);

        //获取文件类型
        int i = fileName.lastIndexOf(".");
        String sufName = fileName.substring(i);
        fileUpload.setFileType(sufName);

        //生成文件保存地址
        String nowDate = sdf.format(new Date());
        String savePath= new File(ResourceUtils.getURL("classpath:").getPath()).getParentFile().getParentFile().getParent()+nowDate;
        fileUpload.setSaveFolder(savePath);


        //新文件名
        String uuid = UUID.randomUUID().toString();
        fileUpload.setNewName(uuid);

        return fileUpload;
    }
}
